package seismosurfer.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import seismosurfer.data.constants.QueryNames;
import seismosurfer.servlets.util.ServletUtil;

/**
 * This servlet sends a page which has an upload
 * form for earthquake related documents to 
 * the web client.
 *
 */
public class UploadFormServlet extends BaseServlet implements QueryNames {
    
    private static final long serialVersionUID = 5104114416053165126L;

    private static final String TYPE = "type";

    // Initialize global variables
    public void init() throws ServletException {
    }

    // Process the HTTP Get request
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        int type;
        try {
            type = Integer.parseInt(request.getParameter(TYPE));
        } catch (NumberFormatException e) {
            type = 0;
        }

        long quakeid;
        try {
            quakeid = Long.parseLong(request.getParameter(QUAKEID));
        } catch (NumberFormatException e) {
            quakeid = 0;
        }

        if (((type != DOC) && (type != URL)) || (quakeid == 0)) {
            System.out.println("Wrong query string!!!\n" + "type=" + type
                    + "&quakeid=" + quakeid);

            ServletUtil.displayUserError(response);
            return;
        }
        request.setAttribute(QUAKEID, new Long(quakeid));

        if (type == DOC) {
            forward("/uploadDOC.jsp", request, response);

        } else {
            forward("/submitURL.jsp", request, response);
        }

    }

    // Clean up resources
    public void destroy() {
    }
}
